<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="系统名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="systemName">
              <!-- <a-input v-model="model.systemName" placeholder="请输入系统名称"  ></a-input> -->
              <a-select @focus="isSystemName" style="width: 100%" placeholder="请选择系统"
                v-model="model.systemName" allowClear>
                <a-select-option v-for="(item, index) in systemNames" :key="index" :value="item.value">
                  {{ item.value }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问题类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="problemType">
              <!-- <a-input v-model="model.problemType" placeholder="请输入问题类型"  ></a-input> -->
              <a-select  @focus="isProblemType" style="width: 100%" placeholder="请选择问题类型"
                v-model="model.problemType" allowClear>
                <a-select-option v-for="(item, index) in problemTypes" :key="index" :value="item.value">
                  {{ item.value }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分配人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="allocator">
              <!-- <a-input v-model="model.allocator" placeholder="请输入分配人"  ></a-input> -->
              <a-select  @focus="isAllocator" @change="allocatorChange" mode="multiple" style="width: 100%" placeholder="请选择分配人"
                v-model="model.allocator" allowClear>
                <a-select-option v-for="(item, index) in allocators" :key="index" :value="item.realname" :label="item.realname">
                  {{ item.realname }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ChSysProblemAllocationForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        systemNames:[],
        problemTypes:[],
        allocators:[],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/ch/chSysProblemAllocation/add",
          edit: "/ch/chSysProblemAllocation/edit",
          queryById: "/ch/chSysProblemAllocation/queryById",
          queryDistListByCode:'/ch/chSysProblemAllocation/queryDistListByCode', //系统问题分配-查询字典列表（系统名称、问题类型）
          queryLoginUsersByDeptId:'/ch/chSysProblemAllocation/queryLoginUsersByDeptId', //查询IT部的所有员工信息

        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    mounted() {
      console.log("mounted");
      // if ( JSON.parse(localStorage.getItem('systemNames')) || JSON.parse(localStorage.getItem('problemTypes'))) {
      //   console.log("JSON.parse(localStorage.getItem('systemNames'))",JSON.parse(localStorage.getItem('systemNames')));
      //   console.log("JSON.parse(localStorage.getItem('problemTypes'))",JSON.parse(localStorage.getItem('problemTypes')));
      //   this.systemNames = JSON.parse(localStorage.getItem('systemNames'))
      //   this.problemTypes = JSON.parse(localStorage.getItem('problemTypes'))
      // }
      //   this.$on('systemNames',data=>{
      //   console.log("systemNames",data);
      //   this.systemNames = data
      // })
      // this.$on('problemTypes',data=>{
      //   console.log("this",this);
      //   console.log("problemTypes",data);
      //   this.problemTypes = data
      // })
     
    },
    methods: {
      // 执行人数据格式化
      allocatorChange(value){
        console.log("value",value);
        console.log("value.join()",value.join())
        // this.model.allocator1 = value.join()
      },
       // 获取系统名称,问题类型
       systemNameAndProblemType() {
        getAction(this.url.queryDistListByCode).then(res => {
            console.log("queryDistListByCode--res", res);
            if (res.code == 200) {
                this.systemNames = res.result.system_code
                this.problemTypes = res.result.problem_type
                // this.$emit('systemNames', res.result.system_code)
                // this.$emit('problemTypes', res.result.problem_type)
                // if (res.result.system_code.length > 0 || res.result.problem_type.length >0) {
                //     localStorage.setItem('systemNames',JSON.stringify(res.result.system_code))
                //     localStorage.setItem('problemTypes',JSON.stringify(res.result.problem_type))
            }
        })
    },
    //获取分配人
    allocator(){
      getAction(this.url.queryLoginUsersByDeptId).then(res => {
        console.log("queryLoginUsersByDeptId--res22", res);
        if (res.code == 200) {
                this.allocators = res.result
                console.log("allocators",this.allocators);
        }
      })
    },
      isSystemName(){
        this.systemNameAndProblemType()
      },
     isProblemType(){
      this.systemNameAndProblemType()
     },
     isAllocator(){
      this.allocator()
     },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model.allocatorList = this.model.allocator
            this.model.allocator="";
            httpAction(httpurl,this.model,method).then((res)=>{
              
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                this.model.allocator=undefined;
                that.$message.warning(res.message);
                
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>